const connection = require('../app/database')

class FileService {
  async createAvatar(userId, mimetype, filename, size) {
    const statement = `
      INSERT INTO avatar (user_id, filename, mimetype,size) VALUES(?,?,?,?)
    `
    const [result] = await connection.execute(statement, [userId, filename, mimetype, size])
    return result
  }
  async getAvatarByUserId(userId) {
    const statement = `SELECT * FROM avatar WHERE user_id = ?;`
    const [result] = await connection.execute(statement, [userId])
    return result[0]
  }
  async createFile(filename, mimetype, size, userId, momentId) {
    const statement = `
      INSERT INTO file (user_id, filename, mimetype,size, moment_id) VALUES(?,?,?,?,?);
    `
    const [result] = await connection.execute(statement, [userId, filename, mimetype, size, momentId])
    return result
  }
  async getFileByFilename(filename) {
    const statement = `SELECT * FROM file WHERE filename = ?;`
    const [result] = await connection.execute(statement, [filename])
    return result[0]
  }
}

module.exports = new FileService()